<?php

namespace app\home\service;

use app\common\helpers\PayHelper;
use app\home\model\Order;
use app\home\model\UserOrder;
use think\Config;

/**
 * 那娃聚合
 */
class NaWa implements IPay
{
    private $config = null;
    private $helper = null;

    public function __construct($config)
    {
        $this->config = $config;
        $this->helper = PayHelper::getInstance($this->config['channel']);
    }

    public function createOrder($params)
    {
        return
        // 计费点映射关系
        $map_class = Config::get('MapPayClass');
        if(!isset($map_class[$params['channel']])){
            $this->helper->record('channel map not found');
            return false;
        }

        $orderModel = new UserOrder();
        $data = $orderModel->create_order_old($params['userid'], $params['channel'], $params['stroeid'], 5,null);

        $info = json_decode($data, true);
        $return['orderid'] = $info['orderid'];
        $return['product_id'] = $params['stroeid'];
        $return['product_price'] = $info['price'];
        $return['product_name'] = $info['subject'];

        $data_Arr['code'] = 'success';
        $data_Arr['data'] = $return;

        return $data_Arr;
    }
    public function processNotify($params)
    {
        $split_str = str_repeat('-',80);
        $this->helper->record($split_str);
        $this->helper->record($params);

        if(empty($params['sign']) || empty($params['orderid']) || empty($params['other'])){
            $this->helper->record('some params empty');
            return json_encode([
                "code"=>16,
                "msg"=>"参数不全"
            ]);
        }

        // 验证签名
        if($this->verifySign($params) == false){
            $this->helper->record('verify fail');
            return json_encode([
                "code"=>5,
                "msg"=>"签名错误"
            ]);
        }
        $this->helper->record('verify ok');

        $order_info = (new Order)->where('id', $params['other'])->find();

        if (empty($order_info)) {
            $this->helper->record('order empty');
            return json_encode([
                "code"=>18,
                "msg"=>"订单找不到"
            ]);
        }

        if ($order_info['status'] == 1) {
            $this->helper->record('order status error');
            return json_encode([
                "code"=>11,
                "msg"=>"订单重复"
            ]);
        }

        // 发货
        $time = date('Y-m-d H:i:s', time());
        $re = Db_connect_server_pay_domian($params['other'], $params['money'], $time);
        if ($re == 'success') {
            return json_encode([
                "code"=>1,
                "msg"=>"ok"
            ]);
        }

        $this->helper->record('delivery fail');
        return json_encode([
            "code"=>18,
            "msg"=>"发货失败"
        ]);
    }

    private function verifySign($data)
    {
        unset($data['channel']);

        $cal_sign = md5($data['pid'].'#'.$data['gid'].'#'.$data['sid'].'#'.$data['uid'].'#'.$data['time'].'#'.$data['orderid'].'#'.$data['money'].'#'.$this->config['app_key']);
        if($cal_sign != $data['sign']){
            return false;
        }

        return true;
    }
}
